package com.hnjowe.util;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @ACTION类: 文件上传（可支持多个文件）
 * @projectname：cocotour
 * @packagename：com.duomi.util
 * @filename：FileUploadAction.java
 * @date：2011-1-21 下午04:18:47
 * @author: wangxiuwu
 */
public class FileUploadAction extends ActionSupport {
	private static Logger log = Logger.getLogger(FileUploadAction.class.getName());
    private static final long serialVersionUID = 572146812454l;

	private List<File>   upload; 			//上传文件域对象
	private List<String> uploadFileName;  	//上传文件名
	private List<String> uploadContentType; //上传文件类型
	
	private String 		 saveDir;			//保存文件的目录路径(通过依赖注入)
	private List<String> filePath;			//文件路径(文件名)：返回

    public String upload()throws Exception{
    	
    	int size = upload.size();
    	if (size == 0)return ERROR;

    	String sDirPath = ServletActionContext.getServletContext().getRealPath("/");
    	String dstname = null;
    	for(int i=0; i<size; i++) {    		
    		dstname = this.getSaveDir() + "/" + generateFileName(uploadFileName.get(i));
	    	File dstFile = new File(sDirPath + dstname);
	    	
	    	FileUtils.copyFile(upload.get(i), dstFile);
	    	log.info("Success to upload file:"+dstFile);
	    	
	    	filePath.add(dstname);
    	}
        return SUCCESS;
    }
    
    /**
	 * 自动分配文件名称
	 * @param fileName： yyMMddHHmmssXXXXX.ext
	 * @return String
	 */
    private String generateFileName(String fileName) {
        // 获得当前时间
        DateFormat format = new SimpleDateFormat("yyMMddHHmmss");
        // 转换为字符串
        String formatDate = format.format(new Date());
        // 随机生成文件编号
        int random = new Random().nextInt(10000);
        // 获得文件后缀名称
        int position = fileName.lastIndexOf(".");
        String extension = fileName.substring(position);
        // 组成一个新的文件名称
        return formatDate + random + extension;
    }

	public List<File> getUpload() {
		return upload;
	}

	public void setUpload(List<File> upload) {
		this.upload = upload;
	}

	public List<String> getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(List<String> uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public List<String> getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(List<String> uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public List<String> getFilePath() {
		return filePath;
	}

	public void setFilePath(List<String> filePath) {
		this.filePath = filePath;
	}

	public String getSaveDir() {
		return saveDir;
	}

	public void setSaveDir(String saveDir) {
		this.saveDir = saveDir;
	}


}

